package com.thread.threadfirst.javaBase.java8;

import lombok.extern.slf4j.Slf4j;

/**
 * lambda
 */
@Slf4j
public class LambdaTest {

    private static String salutation="hello";

    private interface MathOperation {
        int operation(int a, int b);
    }
    private interface GreetingService {
        void sayMessage(String message);
    }

    public static int operate(int a,int b,MathOperation mathOperation){
        return mathOperation.operation(a,b);
    }

    public static void main(String[] args) {
        MathOperation addition =new MathOperation() {
            @Override
            public int operation(int a, int b) {
                return a+b;
            }
        };
        //==========相当于如下===========

        // 类型声明
        MathOperation subtraction =(int a,int b)->a-b;

        // 大括号中的返回语句
        MathOperation multiplication =(a,b)->{
            return a*b;
        };

        // 不用类型声明
        MathOperation division=(a,b)->a/b;

        System.out.println("1+2= "+operate(1,2,addition));
        System.out.println("2-3= "+operate(2,3,subtraction));
        System.out.println("3*4= "+operate(3,4,multiplication));
        System.out.println("4/2= "+operate(4,2,division));

        // 不用括号
        GreetingService greetingService1=message -> System.out.println("hello1："+message);
        // 用括号
        GreetingService greetingService2=(message) -> System.out.println("hello2："+message);

        greetingService1.sayMessage("luffy");
        greetingService2.sayMessage("zoro");

        System.out.println("======访问局部变量=========");

        GreetingService greetingService3=message -> {
            System.out.println(salutation + message);
            message="你好";
            System.out.println(salutation + message);
        };
        greetingService3.sayMessage("sanji");


    }
}
